/**
 * Created by zqk on 2018/08/06.
 */
$(document).ready(function () {
    var iTaskId = addressUri.getParam('iTaskId');
    var ifTaskStats = addressUri.getParam('ifTaskStats');
    var publicTasks = $('#publicTasks');
    publicTasks.datagrid({
        url:moduleUrl+'task/getPublicTasks',
        queryParams: {
            iTaskId:iTaskId,
        },
        method: 'post',
        rownumbers: true,
        pagination: true,
        pageSize: 20,
        pageList: [20,50,100,200],
        idField: 'iTaskId',
        singleSelect:false,
        columns:[[
            {field:'ck',checkbox:true},
            {field:'iTaskId',title:'iTaskId',hidden:true},
            {field:'sTaskName',title:'任务名称',width:100},
            {field:'sTaskContent',title:'任务内容',width:350},
            {field:'sStaffName',title:'执行人',width:100},
        ]],
        onLoadSuccess:function () {
        $.ajax({
            url:moduleUrl+'task/getCheckedTasks',
            data:{
                iTaskId:iTaskId,
            },
            type:'POST',
            dataType:'json',
            success:function (data) {
                for(x in data){
                    $('#publicTasks').datagrid('selectRecord',data[x]);
                }
            },
            error:function (data) {
                console.log(data);
            }
        });
    }
    });

    $('#bt_list_search').click(function(){
        var sTaskName = $('#sTaskName').val();//获取input值
        publicTasks.datagrid('load',{sTaskName:sTaskName,iTaskId:iTaskId});
    });
    // $('#sTaskName').onkeyup(function(e){
    //     var sTaskName = $('#sTaskName').val();//获取input值
    //     publicTasks.datagrid('load',{sTaskName:sTaskName,iTaskId:iTaskId});
    // });

    $('#bt_modal_confirm').click(function () {
        xhExtra.setBtnLoading($('#bt_modal_confirm',parent.document));
        $('#bt_modal_confirm',parent.document).addClass('disabled');
        myWindow.initPopUp({title: '关联任务', text: '确认添加任务吗?'},function () {
            var selections = publicTasks.datagrid('getSelections');
            var iTaskIds = [];
            for(k in selections ){
                iTaskIds.push(selections[k].iTaskId)
            }
            var data = {
                'iTaskIds':iTaskIds,
                'iTaskId':iTaskId,
            }
            $.ajax({
                url:moduleUrl+'task/addRelatedTasks',
                dataType: "JSON",
                data:data,
                type:'POST',
                success:function (data) {
                    window.top.xhTop.showSuccess(
                        data.msg
                    );
                    window.top.xhTop.closeModal();
                    if(!ifTaskStats) {
                        window.top.xhTop.getActiveFrame().taskList.treegrid('reload');
                    }else{
                        window.top.xhTop.getActiveFrame().obj.taskList.treegrid('reload');
                    }

                },
                error:function (data) {
                    window.top.xhTop.showError(
                        data.responseJSON.error,
                        data.responseJSON.msg
                    );
                }
            });
        })

    });

    $('#bt_modal_close').click(function () {
        window.top.xhTop.closeModal();
    })

});